package editor
{
	import Classes.pi_events;
	import Classes.pi_images;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_imageButton;
	import UIFree.controls.uif_scrollArea;
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	import UIFree.uif_transition;
	
	import api_servers.pi_servers;
	
	import flash.display.Loader;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;

	public class pi_buttonLayer extends uif_ecomponent {
		private var _sendButton	: uif_imageButton;
		private var _pluginList	: uif_scrollArea;
		private var buttonHeight: int;
		
		public function pi_buttonLayer(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			this.visible = false;
			_sendButton = new uif_imageButton(this, pi_servers.IMAGESURL + pi_images.i_sendButton, 2, 140, function(): void {
				(app as photo_inject).sendCard();
			});
			_pluginList = new uif_scrollArea(this, 0, 0, awidth, 140);
			var eventIndex: int = addEvent(pi_events.COMPLETEFULLLOAD, doLoad, app);
			function doLoad(e: ObjectEvent): void {
				if ((app as photo_inject).editor) {
					slideShow();
					refreshPluginButtons();
					removeEvent(eventIndex);
				}
			};
			
			var b_count: int = 0;
			for each (var obj: Object in pj_extendServices.list)
				if (obj.icon) b_count++; 
			
			buttonHeight = _pluginList.height / b_count;
			/*addEvent(pi_events.PLUGINSCOMPLETE, refreshPluginButtons, app);*/
		}
		
		protected function get extCount(): int {
			return _pluginList.area.numChildren;
		}
		
		public function refreshPluginButtons(): void {
			_pluginList.area.resize(_pluginList.area.width, extCount * buttonHeight);
			new uif_Timer(300, function(e: TimerEvent): void {
				var b: uif_gbutton = _pluginList.area.getChildAt((e.target as Timer).currentCount - 1) as uif_gbutton;
				b.fx('x', b.x, 0, uif_transition.easeOut(uif_transition.Bounce, 1), 50, 1000).start();
			}, null, extCount);
		}
		
		public function createExtButton(a_sort: uint, caption: String, clickEvent: Function = null, data: Object =null): uif_gbutton {
 			var button: uif_gbutton = new uif_gbutton(_pluginList.area, 255, a_sort * buttonHeight, 249, buttonHeight - 5, 'button-glow');
 			button.text = caption;
 			button.textField.x = 37;
 			var loader: Loader = new Loader();
 			loader.load(new URLRequest(pi_servers.IMAGESURL + data.icon));
 			loader.x = 10;
 			loader.y = (buttonHeight - 20) / 2;
 			button.addChildAt(loader, 0);
 			
 			if (clickEvent != null) button.addEvent(MouseEvent.CLICK, clickEvent);
 			return button;			
		}
		
		public function getExtButton(serviceName: String): uif_gbutton {
			for (var i: int=0; i<_pluginList.area.numChildren; i++) {
				var button: uif_gbutton = _pluginList.area.getChildAt(i) as uif_gbutton;
				if (button && (button._data == serviceName))
					return button;
			}
			return null;
		}
		
		public function get sendButton(): uif_imageButton {
			return _sendButton;
		} 
	}
}